<template>
  <div class="score-comparison">
    <div class="module-title">成绩对比</div>
    <div class="list-container">
      <div class="list-header">
        <div class="header-cell">考试名称</div>
        <div class="header-cell">全队平均分</div>
        <div class="header-cell">一组平均分</div>
        <div class="header-cell">二组平均分</div>
        <div class="header-cell">三组平均分</div>
        <div class="header-cell">个人平均分</div>
      </div>
      <div class="list-body">
        <div v-if="loading" class="loading">
          <div class="loading-spinner"></div>
          <p>加载中...</p>+
        </div>
        <div v-else-if="scoreList.length === 0" class="no-data">
          暂无成绩数据
        </div>
        <div v-else v-for="(item, index) in scoreList" :key="index" class="list-row">
          <div class="list-cell">{{ item.examName }}</div>
          <div class="list-cell">{{ item.allAvg }}</div>
          <div class="list-cell">{{ item.groupOneAavg }}</div>
          <div class="list-cell">{{ item.groupTwoAavg }}</div>
          <div class="list-cell">{{ item.groupThreeAavg }}</div>
          <div class="list-cell">{{ item.avgScore }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ScoreComparison',
  data() {
    return {
      scoreList: [],
      loading: true,
      retryCount: 0
    };
  },
  mounted() {
    if (this.$displayapi) {
      this.$nextTick(() => {
        this.loadScoreData();
      });
    }
  },
  activated() {
    if (this.$displayapi) {
      this.$nextTick(() => {
        this.loadScoreData();
      });
    }
  },
  watch: {
    '$displayapi': {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.$nextTick(() => {
            this.loadScoreData();
          });
        }
      }
    }
  },
  methods: {
    async loadScoreData() {
      if (this.loading) return;
      this.loading = true;
      try {
        const response = await this.$displayapi.getScoreCompare();
        if (response && response.code === "200" && Array.isArray(response.data)) {
          // 定义考试的逻辑时间顺序 (从小到大)，用于排序
          const examOrder = {
            '第一次月考': 1,
            '期中测试': 2, // 期中测试在第一次月考之后
            '第二次月考': 3,
            '期末考试': 4
          };

          // 对后端返回的数据进行排序处理
          // 按照逻辑时间顺序升序排列 (时间靠前的排在前面)
          const sortedData = response.data.sort((a, b) => {
            const orderA = examOrder[a.examName] || 999; // 不在预定义顺序中的排在最后
            const orderB = examOrder[b.examName] || 999;

            return orderA - orderB; // 升序排序，使时间靠前的排在前面
          });

          // 将排序后的数据映射到 scoreList
          // 这里不再进行 reverse() 操作，以保持时间升序排列
          this.scoreList = sortedData.map(item => ({
            examName: item.examName || '',
            allAvg: item.allAvg || 0,
            groupOneAavg: item.groupOneAavg || 0,
            groupTwoAavg: item.groupTwoAavg || 0,
            groupThreeAavg: item.groupThreeAavg || 0,
            avgScore: item.avgScore || 0
          }));

          this.retryCount = 0;
        } else {
          this.scoreList = [];
          this.retryLoad();
        }
      } catch (error) {
        console.error('获取成绩对比数据失败:', error);
        this.scoreList = [];
        this.retryLoad();
      } finally {
        this.loading = false;
      }
    },
    retryLoad() {
      if (this.retryCount < 3) {
        this.retryCount++;
        setTimeout(() => {
          this.loadScoreData();
        }, 1000);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.score-comparison {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .module-title {
    font-size: 22px;
    color: #68d8fe;
    margin-bottom: 15px;
    font-weight: bold;
  }

  .list-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .list-header {
      display: flex;
      background-color: rgba(104, 216, 254, 0.1);
      border-bottom: 1px solid rgba(104, 216, 254, 0.5);

      .header-cell {
        flex: 1;
        padding: 10px;
        text-align: center;
        color: #68d8fe;
        font-weight: bold;
        font-size: 18px;
      }
    }

    .list-body {
      flex: 1;
      overflow-y: auto;
      max-height: 260px;

      &::-webkit-scrollbar {
        width: 5px;
      }

      &::-webkit-scrollbar-track {
        background: rgba(19, 25, 47, 0.5);
      }

      &::-webkit-scrollbar-thumb {
        background-color: rgba(104, 216, 254, 0.5);
        border-radius: 3px;
      }

      .loading {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 200px;

        .loading-spinner {
          width: 40px;
          height: 40px;
          border: 3px solid rgba(104, 216, 254, 0.3);
          border-radius: 50%;
          border-top-color: #68d8fe;
          animation: spin 1s linear infinite;
          margin-bottom: 10px;
        }

        p {
          color: #d3d6dd;
        }

        @keyframes spin {
          to { transform: rotate(360deg); }
        }
      }

      .no-data {
        text-align: center;
        padding: 20px;
        color: #909399;
        font-style: italic;
      }

      .list-row {
        display: flex;
        border-bottom: 1px solid rgba(104, 216, 254, 0.1);

        &:hover {
          background-color: rgba(104, 216, 254, 0.05);
        }

        .list-cell {
          flex: 1;
          padding: 10px;
          text-align: center;
          color: #d3d6dd;
          font-size: 18px;
        }
      }
    }
  }
}
</style>